package com.wjk.easy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wjk.easy.R;

public class ExamGroupAdapter extends RecyclerView.Adapter {
    private Context context;
    private int groupNum;
    private int mSelectedPos=0;

    public ExamGroupAdapter(Context context, int groupNum) {
        this.context = context;
        this.groupNum = groupNum;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_group, parent,
                false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        MyViewHolder h = (MyViewHolder) holder;
        h.tv_group_name.setText("第"+(position+1)+"组");
        h.tv_group_name.setSelected(position==mSelectedPos);
        h.tv_group_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(changeLister!=null){
                    changeLister.onGroupChange(position+1);
                }
                if(position!=mSelectedPos){
                    mSelectedPos=position;
                    notifyDataSetChanged();
                }
            }
        });

    }


    @Override
    public int getItemCount() {
        return groupNum;
    }

    public void setGroupNum(int groupNum,int selectPos) {
        this.groupNum = groupNum;
        this.mSelectedPos = selectPos-1;
        notifyDataSetChanged();
    }

    public int getGroupNum() {
        return this.groupNum;
    }

    /**
     * ViewHolder的类，用于缓存控件
     */
    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_group_name;

        MyViewHolder(View view) {
            super(view);
            tv_group_name = view.findViewById(R.id.tv_group_name);
        }
    }

    public void setGroupChangeListener(GroupChangeLister changeLister){
        this.changeLister = changeLister;
    }

    GroupChangeLister changeLister;
    public interface GroupChangeLister{
        void onGroupChange(int groupIdx);
    }
}
